package by.gravity.pvh.fragments;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.widget.EditText;
import org.holoeverywhere.widget.LinearLayout;
import org.holoeverywhere.widget.Spinner;
import org.holoeverywhere.widget.Toast;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import by.gravity.pvh.R;
import by.gravity.pvh.data.SQLDataManager.TypeOrder;
import by.gravity.pvh.model.CommonModel;
import by.gravity.pvh.model.WindowModel;
import by.gravity.pvh.utils.Configuration;
import by.gravity.pvh.utils.Formula;
import by.gravity.pvh.utils.Formula.TypeWindow;

public class WindowFragment extends CommonFragment {

	private EditText width;

	private EditText height;

	private RadioGroup typeOpenWindowGroup;

	private RadioGroup typeWindowGroup;

	private EditText countSimpleOpen;

	private EditText countWithWind;

	private EditText countDeafParts;

	private EditText underWindowLength;

	private Spinner underWindowWidth;

	private EditText windowEbbDownLength;

	private Spinner windowEbbDownWidth;

	private EditText workPrice;

	private RadioGroup additionalWindowType;
	
	private LinearLayout aditionalTypeWindowLayout;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		init();
	}

	private void init() {

		width = (EditText) getView().findViewById(R.id.windowWidth);
		height = (EditText) getView().findViewById(R.id.windowHeight);
		typeOpenWindowGroup = (RadioGroup) getView().findViewById(R.id.typeOpenWindowGroup);
		typeWindowGroup = (RadioGroup) getView().findViewById(R.id.typeWindowGroup);
		countSimpleOpen = (EditText) getView().findViewById(R.id.countTypeSimpleOpen);
		countWithWind = (EditText) getView().findViewById(R.id.countTypeWind);
		countDeafParts = (EditText) getView().findViewById(R.id.countDeafParts);
		underWindowLength = (EditText) getView().findViewById(R.id.underWindowLength);
		underWindowWidth = (Spinner) getView().findViewById(R.id.underWindowWidth);
		underWindowWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));

		windowEbbDownLength = (EditText) getView().findViewById(R.id.windowEbbDownLength);
		windowEbbDownWidth = (Spinner) getView().findViewById(R.id.windowEbbDownWidth);
		windowEbbDownWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getEbbwidth()));

		workPrice = (EditText) getView().findViewById(R.id.workPrice);

		typeWindowGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				if (!getCountWindow().equals("")) {
					int countWindow = getIntValue(getCountWindow());
					showWindowImage(countWindow);
					if (countWindow == 2) {
						aditionalTypeWindowLayout.setVisibility(View.VISIBLE);
					} else {
						aditionalTypeWindowLayout.setVisibility(View.GONE);
					}
				} else {
					aditionalTypeWindowLayout.setVisibility(View.GONE);
				}
			}
		});

		additionalWindowType = (RadioGroup) getView().findViewById(R.id.aditionalTypeWindow);
		
		aditionalTypeWindowLayout = (LinearLayout) getView().findViewById(R.id.aditionalTypeWindowLayout);

	}

	protected String getFormula() {

		String width = this.width.getText().toString();
		String height = this.height.getText().toString();
		TypeWindow typeWindow = getTypeWindow();
		if (typeWindow == null) {
			Toast.makeText(getActivity(), R.string.notSelectedTypeWindow, Toast.LENGTH_SHORT).show();
			return null;
		}
		String countWindow = getCountWindow();
		if (countWindow.equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectedWindow, Toast.LENGTH_SHORT).show();
			return null;
		}

		String countSimpleOpen = this.countSimpleOpen.getText().toString();
		String countWithWind = this.countWithWind.getText().toString();
		String countDeafParts = this.countDeafParts.getText().toString();
		String underwindowLength = underWindowLength.getText().toString();
		String underwindowWidth = String.valueOf(underWindowWidth.getSelectedItemPosition() + 1);
		String ebbDownLength = windowEbbDownLength.getText().toString();
		String ebbDownWidth = String.valueOf(windowEbbDownWidth.getSelectedItemPosition() + 1);

		String workPrice = this.workPrice.getText().toString();

		if (countSimpleOpen.length() == 0) {
			countSimpleOpen = "0";
		}
		if (countWithWind.length() == 0) {
			countWithWind = "0";
		}
		if (countDeafParts.length() == 0) {
			countDeafParts = "0";
		}

		if (getIntValue(countWindow) != getIntValue(countSimpleOpen) + getIntValue(countWithWind) + getIntValue(countDeafParts)) {
			Toast.makeText(getActivity(), R.string.windowCountError, Toast.LENGTH_SHORT).show();
			return null;
		}

		if (getIntValue(width) > getIntValue(underwindowLength)) {
			Toast.makeText(getActivity(), R.string.underwindowLengthLess, Toast.LENGTH_SHORT).show();
			return null;
		}

		if (getIntValue(width) > getIntValue(ebbDownLength)) {
			Toast.makeText(getActivity(), R.string.ebbLengthLess, Toast.LENGTH_SHORT).show();
			return null;
		}

		if (countSimpleOpen.length() != 0 || countWithWind.length() != 0) {
			return Formula.window(width, height, typeWindow, countWindow, countSimpleOpen, countWithWind, underwindowLength,
					underwindowWidth, ebbDownLength, ebbDownWidth, workPrice);
		} else {
			return Formula.deafWindow(width, height, typeWindow, countDeafParts, underwindowLength, underwindowWidth, ebbDownLength,
					ebbDownWidth, workPrice);
		}

	}

	private String getCountWindow() {

		String countWindow = getValue(this.typeWindowGroup);
		if (countWindow.equals(getString(R.string.oneOpen))) {
			countWindow = "1";
		} else if (countWindow.equals(getString(R.string.twoOpen))) {
			countWindow = "2";
		} else if (countWindow.equals(getString(R.string.threeOpen))) {
			countWindow = "3";
		} else if (countWindow.equals(getString(R.string.fourOpen))) {
			countWindow = "4";
		}

		return countWindow;

	}

	private TypeWindow getTypeWindow() {

		String typeWindowString = getValue(this.typeOpenWindowGroup);
		if (typeWindowString.equals("")) {
			return null;
		}
		TypeWindow typeWindow = typeWindowString.equals(getString(R.string.oneChamger)) ? TypeWindow.ONE_CHAMGER : TypeWindow.TWO_CHAMGER;
		return typeWindow;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return inflater.inflate(R.layout.f_window, null);
	}

	@Override
	protected CommonModel getModel() {

		WindowModel model = new WindowModel();
		model.setCountDeafParts(getValue(countDeafParts));
		model.setCountSimpleOpen(getValue(countSimpleOpen));
		model.setCountWindow(getValue(this.typeWindowGroup));
		model.setCountWithWind(getValue(countWithWind));
		model.setEbbDownLength(getValue(windowEbbDownLength));
		model.setEbbDownWidth(getValue(windowEbbDownWidth));
		model.setHeight(getValue(height));
		model.setUnderwindowLength(getValue(underWindowLength));
		model.setTypeWindow(getValue(this.typeOpenWindowGroup));
		model.setUnderwindowWidth(getValue(underWindowWidth));
		model.setWidth(getValue(width));
		model.setWorkPrice(getValue(workPrice));
		model.setAditionalTypeWindow(getValue(additionalWindowType));

		return model;
	}

	@Override
	protected void init(CommonModel commonModel) {

		WindowModel model = (WindowModel) commonModel;
		setValue(countDeafParts, model.getCountDeafParts());
		setValue(countSimpleOpen, model.getCountSimpleOpen());
		setValue(typeWindowGroup, model.getCountWindow());
		setValue(countWithWind, model.getCountWithWind());
		setValue(windowEbbDownLength, model.getEbbDownLength());
		setValue(windowEbbDownWidth, model.getEbbDownWidth());
		setValue(height, model.getHeight());
		setValue(typeOpenWindowGroup, model.getTypeWindow());
		setValue(underWindowWidth, model.getUnderwindowWidth());
		setValue(underWindowLength, model.getUnderwindowLength());
		setValue(width, model.getWidth());
		setValue(workPrice, model.getWorkPrice());
		setValue(additionalWindowType, model.getAditionalTypeWindow());

	}

	@Override
	protected TypeOrder getTypeOrder() {

		return TypeOrder.WINDOW;
	}

	@Override
	protected void clear() {

		setEmptyValue(countDeafParts);
		setEmptyValue(countSimpleOpen);
		setEmptyValue(typeWindowGroup);
		setEmptyValue(countWithWind);
		setEmptyValue(windowEbbDownLength);
		setEmptyValue(windowEbbDownWidth);
		setEmptyValue(height);
		setEmptyValue(typeOpenWindowGroup);
		setEmptyValue(underWindowWidth);
		setEmptyValue(underWindowLength);
		setEmptyValue(width);
		setEmptyValue(workPrice);
		setEmptyValue(additionalWindowType);
	}

}
